<template>
  <div class="menu-manage">
    <!-- 查询区域 -->
    <div class="query-form">
      <el-form :inline="true" :model="queryForm" ref="refQueryForm">
        <el-form-item label="菜单名称:" prop="menuName">
          <el-input v-model="queryForm.menuName" placeholder="请输入菜单名称"></el-input>
        </el-form-item>
        <el-form-item label="菜单状态:" prop="menuState">
          <el-select v-model="queryForm.menuState" placeholder="请选择菜单状态">
            <el-option label="正常" :value="1"></el-option>
            <el-option label="停用" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="queryFormSubmit" v-auth="'menu-query'">查询</el-button>
          <el-button @click="restForm('refQueryForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格 -->
    <div class="base-table">
      <!-- 表格头部操作区 -->
      <div class="action" v-auth="'menu-add'">
        <el-button type="primary" @click="addMenu(1)">创建菜单</el-button>
      </div>
      <!-- 用户列表 -->
      <el-table :data="showMenuList" row-key="_id" :expand-row-keys="expandRowKeys">
        <el-table-column
          v-for="colum in colums"
          :key="colum.prop"
          :prop="colum.prop"
          :label="colum.label"
          :formatter="colum.formatter"
          :width="colum.width"
        >
        </el-table-column>
        <el-table-column label="操作" width="210">
          <template #default="scope">
            <el-button type="primary" size="mini" @click="addMenu(2, scope.row)" v-auth="'menu-add'" v-show="scope.row.menuType != 2">新增</el-button>
            <el-button size="mini" @click="editMenu(scope.row)" v-auth="'menu-edit'">编辑</el-button>
            <el-button type="danger" size="mini" @click="deleteMenu(scope.row)" v-auth="'menu-delete'">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 新增菜单弹窗 -->
    <el-dialog title="新增菜单" v-model="showModal" @close="menuModelClose">
      <el-form :model="menuForm" ref="refMenuForm" label-width="100px" :rules="rules">
        <el-form-item label="父级菜单" prop="parentId">
          <el-cascader
            v-model="menuForm.parentId"
            :options="menuList"
            :props="{ checkStrictly: true, value: '_id', label: 'menuName' }"
            placeholder="请选择"
            clearable
          />
          <span class="ml20">不选，则默认创建一级菜单</span>
        </el-form-item>
        <el-form-item label="菜单类型" prop="menuType">
          <el-radio-group v-model="menuForm.menuType">
            <el-radio :label="1" border> 菜单</el-radio>
            <el-radio :label="2" border>按钮</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="菜单名称" prop="menuName">
          <el-input v-model="menuForm.menuName" placeholder="请输入菜单名称" />
        </el-form-item>
        <el-form-item label="菜单图标" prop="icon" v-show="menuForm.menuType == 1">
          <el-input v-model="menuForm.icon" placeholder="请输入菜单图标" />
        </el-form-item>
        <el-form-item label="路由地址" prop="path" v-show="menuForm.menuType == 1">
          <el-input v-model="menuForm.path" placeholder="请输入路由地址" />
        </el-form-item>
        <el-form-item label="权限标识" prop="menuCode" v-show="menuForm.menuType == 2">
          <el-input v-model="menuForm.menuCode" placeholder="请输入权限标识" />
        </el-form-item>
        <el-form-item label="组件路径" prop="component" v-show="menuForm.menuType == 1">
          <el-input v-model="menuForm.component" placeholder="请输入组件路径" />
        </el-form-item>
        <el-form-item label="菜单状态" prop="menuState" v-show="menuForm.menuType == 1">
          <el-radio-group v-model="menuForm.menuState">
            <el-radio :label="1" border>正常</el-radio>
            <el-radio :label="2" border>停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <span>
          <el-button @click="menuModelClose">取 消</el-button>
          <el-button type="primary" @click="menuModelSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import utils from '@/utils/utils.js';
import { mapState } from 'vuex';
export default {
  name: 'Menu',
  computed: {
    ...mapState(['menuList']),
    expandRowKeys() {
      return this.menuList.map((menu) => menu._id);
    },
  },
  data() {
    return {
      // 查询表单
      queryForm: { menuName: '', menuState: 1 },
      // 菜单表单
      menuForm: { menuType: 1, menuState: 1, parentId: [null] },
      // 展示的 菜单列表，默认和menuList 一致，只有条件查询的时候才修改
      showMenuList: [],
      // 菜单表单 校验规则
      rules: {
        menuName: [
          { required: true, message: '请输入菜单名称' },
          { min: 2, max: 10, message: '长度在2-8个字符' },
        ],
      },
      // 弹框显示
      showModal: false,
      // 表单的类型   add   edit  delete
      action: 'add',
      // 菜单表格列选项
      colums: [
        { label: '菜单名称', prop: 'menuName', width: 180 },
        { label: '图标', prop: 'icon' },
        {
          label: '菜单类型',
          prop: 'menuType',
          width: 90,
          formatter(row, column, value) {
            return { 1: '菜单', 2: '按钮' }[value];
          },
        },
        { label: '权限标识', prop: 'menuCode' },
        { label: '路由地址', prop: 'path' },
        { label: '组件路径', prop: 'component' },
        {
          label: '菜单状态',
          prop: 'menuState',
          formatter(row, column, value) {
            return { 1: '正常', 2: '停用' }[value];
          },
        },
        {
          label: '创建时间',
          prop: 'createTime',
          width: 150,
          formatter(row, column, value) {
            return utils.formateDate(new Date(value));
          },
        },
      ],
    };
  },
  mounted() {
    this.getMenuList();
  },
  methods: {
    async getMenuList() {
      let list = await this.$api.getMenuList(this.queryForm);
      if (this.action != 'filter') {
        this.$store.commit('setMenuList', list);
      }
      this.showMenuList = list;
    },
    // 查询表单提交事件
    queryFormSubmit() {
      this.action = 'filter';
      this.getMenuList();
    },
    // 添加 菜单
    addMenu(type, row) {
      this.action = 'add';
      this.showModal = true;
      console.log('==>', row);
      if (type == 2) {
        this.menuForm.parentId = [...row.parentId, row._id].filter((item) => item);
      }
    },
    // 修改 菜单
    editMenu(row) {
      this.action = 'edit';
      this.showModal = true;
      // 在dom更新过后下一个阶段，下一次更新才获取值
      this.$nextTick(() => {
        Object.assign(this.menuForm, row);
      });
    },
    // 删除 菜单
    async deleteMenu({ _id }) {
      await this.$api.menuSubmit({ _id, action: 'delete' });
      this.$message.success('删除成功');
      this.getMenuList();
    },
    // 弹窗关闭事件
    menuModelClose() {
      this.showModal = false;
      this.restForm('refMenuForm');
    },
    // 弹窗提交事件
    menuModelSubmit() {
      this.$refs.refMenuForm.validate(async (valid) => {
        if (valid) {
          let { action, menuForm } = this;
          let param = { ...menuForm, action };
          await this.$api.menuSubmit(param);
          this.$message.success(`菜单${this.action == 'add' ? '添加' : '编辑'}成功`);
          this.menuModelClose();
          this.getMenuList();
        }
      });
    },
    // 重置表单
    restForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style lang="scss"></style>
